#include "exception.h"

#include <stddef.h>

#include <stdio.h>

#include "sqlite3.h"

#include "tags.h"

#include "log/log.h"

static void do_throw(JNIEnv* env, const char* name, const char* message) {
	jclass clazz = env->FindClass(name);
	if (clazz != NULL) {
		env->ThrowNew(clazz, message);
		env->DeleteLocalRef(clazz);
	}
}

void exception_throw_sqlite3(JNIEnv* env, int err) {
	const char* class_name = err == SQLITE_BUSY
			? "com/netease/nimlib/search/model/NIMIndexBusyException"
			: "com/netease/nimlib/search/model/NIMIndexException";

	char message[64] = {0};
	sprintf(message, "%d", err);

	do_throw(env, class_name, message);
}

void exception_throw_param(JNIEnv* env) {
	do_throw(env, "com/netease/nimlib/search/model/NIMIndexParamException", "invalid param");
}
